package com.itheima.com.Controller;

import com.itheima.com.domain.Student;
import com.itheima.com.service.StudentService;

import java.util.Scanner;

public abstract class BaseStudentController {
    private Scanner sc=new Scanner(System.in);
    public void start() {
        while (true) {
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    //   System.out.println("增");
                    addStudent();
                    break;
                case "2":
                   // System.out.println("删");
                    deleteById();
                    break;
                case "3":
                   // System.out.println("改");
                    updateStudent();
                    break;
                case "4":
                //    System.out.println("查");
                    getAllStudent();
                    break;
                case "5":
                    System.out.println("退出学生信息");
                    return;
            }
        }
    }

    public void updateStudent() {
        String uid=inputId();
        Student newStu = inputStuInfo(uid);
        StudentService.updateStudent(uid,newStu);
        System.out.println("修改成功！");
    }


    public void deleteById() {
        String did= inputId();
        StudentService.deleteById(did);
        System.out.println("删除成功！");
    }

    public String inputId() {
        String id;
        while (true){
        System.out.println("输入学号");
        id=sc.next();
        boolean exits = StudentService.isExits(id);
        if (!exits){
            System.out.println("该学号不存在，请重新输入！");
        }else {
            break;
        }
        }
        return id;
    }

    public void getAllStudent() {
        Student[] students=StudentService.gatAllStudent();
        if (students==null){
            System.out.println("无数据，添加后重试！");
            return;
        }
        System.out.println("学号\t\t姓名\t年龄\t生日");
        for (int i = 0; i < students.length; i++) {
            Student stu=students[i];
            if (stu!=null) {
                System.out.println(stu.getId() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t\t" + stu.getBirthday());
            }
        }
    }

    public void addStudent() {
        String id;
        while (true) {
            System.out.println("输入学生学号：");
            id= sc.next();
            boolean flag = StudentService.isExits(id);
            if (flag) {
                System.out.println("该学号已被占用，重新输入");
            } else {
                break;
            }
        }
        Student stu = inputStuInfo(id);
         StudentService.addStudent(stu);
        System.out.println("添加成功！");
    }

    public abstract Student inputStuInfo(String id);
}
